package cn.tedu.carehome.foundation.controller;

import cn.tedu.carehome.foundation.pojo.dto.OlderAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.OlderUpdateDTO;
import cn.tedu.carehome.foundation.pojo.vo.OlderListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.OlderStandardVO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.restful.WebConsts;
import cn.tedu.carehome.foundation.service.IOlderService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/olders")
@Validated
@Api(tags = "老人健康管理模块")
public class OlderController {
    @Autowired
    IOlderService olderService;

    @PostMapping("/add-new")
    @ApiOperation("添加老人健康信息")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid @RequestBody OlderAddNewDTO olderAddNewDTO) {
        log.debug("开始处理【添加老人健康信息】的请求，参数：{}", olderAddNewDTO);
        olderService.addNew(olderAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除老人健康信息")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "老人id", required = true, example = "3", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000000, message = "删除老人健康信息失败，老人id非法")
                             @PathVariable Long id) {
        log.debug("开始处理【根据id删除老人健康信息】的请求，参数：{}", id);
        olderService.delete(id);
        return JsonResult.ok();
    }


    @PostMapping("/deleteByIds")
    @ApiOperation("根据若干id删除老人健康信息")
    @ApiOperationSupport(order = 210)
    public JsonResult<Void> deleteByIds(Long[] ids) {
        /*log.debug("开始处理【根据若干id删除老人健康信息】的请求，参数：{}", ids);*/
        olderService.deleteByIds(ids);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改老人健康信息")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@RequestBody OlderUpdateDTO olderUpdateDTO) {
        log.debug("开始处理【修改老人健康信息】的请求，参数：{}", olderUpdateDTO);
        olderService.updateInfoById(olderUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id老人健康信息")
    @ApiOperationSupport(order = 410)
    public JsonResult<OlderStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询老人健康详情】的请求，ID：{}", id);
        OlderStandardVO standardVO = olderService.getStandardById(id);
        return JsonResult.ok(standardVO);
    }

    @GetMapping("/list")
    @ApiOperation("查询老人健康信息列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<OlderListItemVO>> list() {
        log.debug("开始处理【查询老人健康信息列表】的请求，无参数");
        List<OlderListItemVO> list = olderService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/list-limit")
    @ApiOperation("分页查询老人健康信息列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", required = true, example = "1"),
                        @ApiImplicitParam(name = "size", value = "每页数量", required = true, example = "10"),
                        @ApiImplicitParam(name = "content", value = "搜索的内容", required = true, example = "测试")})
    public JsonResult<List<OlderListItemVO>> listLimit(int page, int size, String content) {
        log.debug("开始处理【分页查询老人健康信息列表】的请求，页数：{}, 显示数量：{}, 查询的内容：{}", page, size, content);
        page = size*(page-1);
        List<OlderListItemVO> listLimit = olderService.listLimit(page, size, content);
        return JsonResult.ok(listLimit);
    }

    @GetMapping("/list/count")
    @ApiOperation("统计查询内容匹配老人数量")
    @ApiOperationSupport(order = 440)
    public int countByContent(String content) {
        log.debug("开始处理【根据查询内容统计老人数量】的请求，参数：{}", content);
        int count = olderService.countByContent(content);
        return count;
    }

    @GetMapping("/list-by-page")
    @ApiOperation("分页查询老人健康信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "page", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "2")
    })
    public JsonResult<JsonPage<OlderListItemVO>> listOlderPage(
                            @RequestParam(required = false, defaultValue = WebConsts.DEFAULT_PAGE) Integer page,
                            @RequestParam(required = false, defaultValue = WebConsts.DEFAULT_PAGE_SIZE) Integer pageSize
    ) {
        JsonPage<OlderListItemVO> olderPage = olderService.listOlderPage(page, pageSize);
        return JsonResult.ok(olderPage);
    }
}
